package gov.pbc.gx.nn.pz.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.exception.NoneLoginException;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.variant.Record;
import com.bstek.dorado.web.DoradoContext;

import gov.pbc.gx.nn.pz.dao.ZykbpzywDao;
import gov.pbc.gx.nn.pz.entity.Account;
import gov.pbc.gx.nn.pz.entity.HcZykbpzyw;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

/**
 * 
 * @author charles.cao 本类主要实现业Zykbpz务逻辑的操作
 * 
 */
@Component("zykbpzywPR")
public class ZykbpzywPR {
	@Resource
	@Autowired
	private ZykbpzywDao zykbpzywDao;

	private Integer PZ_CODE_ = 10;
	private static final Integer Font_Size = 16;

	@Transactional
	@DataProvider
	public Record getZykbpz() {
		try {
			IUser loginuser = ContextHolder.getLoginUser();
			if (loginuser == null)
				return null;
			if (loginuser.getDepts() == null
					|| loginuser.getDepts().size() <= 0)
				return null;
			Record record = new Record();
			record.put("pzdate", new Date());

			Collection<HcZykbpzyw> lists = zykbpzywDao.getMuByDeptName();
			if (lists.isEmpty()) {
				HcZykbpzyw e = new HcZykbpzyw();
				e.setPzDate(new Date());
				e.setPzTemplate("新建模板");
				lists.add(e);
				record.put("zykbpz", lists);
			} else {
				for (HcZykbpzyw hcZykbpzyw : lists) {
					hcZykbpzyw.setPzDate(new Date());
				}
				record.put("zykbpz", lists);
			}

			List<HcpzXY> xjpzXYs = zykbpzywDao.getHcPzXYs(PZ_CODE_);

			if (xjpzXYs != null && !xjpzXYs.isEmpty()) {
				record.put("pys", xjpzXYs);
			} else {
				xjpzXYs = new ArrayList<HcpzXY>();
				HcpzXY xjpzXY = new HcpzXY();
				xjpzXY.setProperty("页边距", 50, 50, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setId(UUID.randomUUID().toString());
				xjpzXY.setProperty("年", 255, 68, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("月", 325, 68, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("日", 385, 68, Font_Size);
				xjpzXYs.add(xjpzXY);

				// xjpzXY = new HcpzXY();
				// xjpzXY.setProperty("流水号", 560, 68);
				// xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("表外账户名称", 110, 100, Font_Size);
				xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("表外账户账号", 110, 125, Font_Size);
				xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("起始号码", 115, 150, Font_Size);
				xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("终止号码", 400, 150, Font_Size);
				xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("份数", 650, 150, Font_Size);
				xjpzXYs.add(xjpzXY);
				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("凭证种类代码", 130, 170, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("业务种类代码", 130, 200, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("冲正/补记明细业务种类代码", 130, 250, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("关联账户名称", 110, 310, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("关联账户账号", 110, 345, Font_Size);
				xjpzXYs.add(xjpzXY);

				xjpzXY = new HcpzXY();
				xjpzXY.setProperty("原账务日期", 480, 380, Font_Size);
				xjpzXYs.add(xjpzXY);

			}

			record.put("pys", xjpzXYs);
			// record.put("pym", pym);

			return record;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public void setImageData(Graphics2D graphics, Map<String, Object> param) {
		HcZykbpzyw hcZykbpzyw = (HcZykbpzyw) param.get("zykbpz");
		Date pzdate = (Date) param.get("pzdate");
		String fontzh = param.get("fontzh").toString();
		String fonten = param.get("fonten").toString();
		String click = "√";
		int pyx = (int) Integer.parseInt(param.get("x").toString());
		int pyy = (int) Integer.parseInt(param.get("y").toString());
		// int pyx = 0, pyy = 0;
		// Map<String, Object> pym = (Map<String, Object>) param.get("pym");
		graphics.setBackground(Color.WHITE);
		graphics.setColor(Color.BLACK);
		List<HcpzXY> hcpzxys = (List<HcpzXY>) param.get("pys");
		for (HcpzXY hcpzXY : hcpzxys) {
			int x = hcpzXY.getX() + pyx;
			int y = hcpzXY.getY() + pyy;
			int size = hcpzXY.getFontSize();
			String lable = hcpzXY.getLabel();
			if (pzdate != null) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(pzdate);
				if (lable.equals("年")) {
					Font enFont = new Font(fonten, Font.PLAIN, size);
					graphics.setFont(enFont);
					graphics.drawString(
							String.format("%04d", calendar.get(Calendar.YEAR)),
							x - 5, y);
				}
				if (lable.equals("月")) {
					Font enFont = new Font(fonten, Font.PLAIN, size);
					graphics.setFont(enFont);
					graphics.drawString(String.format("%02d",
							calendar.get(Calendar.MONTH) + 1), x, y);
				}
				if (lable.equals("日")) {
					Font enFont = new Font(fonten, Font.PLAIN, size);
					graphics.setFont(enFont);
					graphics.drawString(
							String.format("%02d", calendar.get(Calendar.DATE)),
							x, y);
				}
			}
			if (hcZykbpzyw != null) {
				if (lable.equals("表外账户名称")) {
					if (hcZykbpzyw.getTbName() != null) {
						Font zhFont = new Font(fontzh, Font.PLAIN, size);
						graphics.setFont(zhFont);
						graphics.drawString(hcZykbpzyw.getTbName(), x, y - 5);
					}
				}
				if (lable.equals("表外账户账号")) {
					if (hcZykbpzyw.getTbAccount() != null) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						graphics.drawString(hcZykbpzyw.getTbAccount(), x, y);
					}
				}
				if (lable.equals("关联账户名称")) {
					if (hcZykbpzyw.getRelatedName() != null) {
						Font zhFont = new Font(fontzh, Font.PLAIN, size);
						graphics.setFont(zhFont);
						graphics.drawString(hcZykbpzyw.getRelatedName(), x,
								y - 5);
					}
				}
				if (lable.equals("关联账户账号")) {
					if (hcZykbpzyw.getRelatedAccount() != null) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						graphics.drawString(hcZykbpzyw.getRelatedAccount(), x,
								y - 5);
					}
				}
				if (lable.equals("份数")) {
					if (hcZykbpzyw.getFenShu() != 0) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						graphics.drawString(
								String.valueOf(hcZykbpzyw.getFenShu()), x - 10,
								y - 2);
					}
				}
				if (lable.equals("起始号码")) {
					if (hcZykbpzyw.getStartAccount() != null) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						String num = hcZykbpzyw.getStartAccount();
						graphics.drawString(num.substring(0, 4) + " ", x, y - 2);
						graphics.drawString(num.substring(4, 8) + " ", 55 + x,
								y - 2);
						graphics.drawString(num.substring(8, 12) + " ",
								110 + x, y - 2);
						graphics.drawString(num.substring(12, 16) + " ",
								165 + x, y - 2);
					}
				}
				if (lable.equals("终止号码")) {
					if (hcZykbpzyw.getEndAccount() != null) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						String num = hcZykbpzyw.getEndAccount();
						graphics.drawString(num.substring(0, 4) + " ", x - 5,
								y - 2);
						graphics.drawString(num.substring(4, 8) + " ", 50 + x,
								y - 2);
						graphics.drawString(num.substring(8, 12) + " ",
								105 + x, y - 2);
						graphics.drawString(num.substring(12, 16) + " ",
								160 + x, y - 2);
					}
				}
				// if (lable.equals("流水号")) {
				// if (hcZykbpzyw.getSerialnumber() != null) {
				// graphics.setFont(enFont);
				// graphics.drawString(hcZykbpzyw.getSerialnumber(), x, y);
				// }
				// }
				// if (hcZykbpzyw.getYzwDate() != null) {
				// Calendar calendar1 = Calendar.getInstance();
				// calendar1.setTime(hcZykbpzyw.getYzwDate());
				// if (lable.equals("年")) {
				// Font enFont = new Font(fonten, Font.PLAIN, size);
				// graphics.setFont(enFont);
				// graphics.drawString(String.format("%04d",
				// calendar1.get(Calendar.YEAR)), 220 + x, 310 + y);
				// }
				// if (lable.equals("月")) {
				// Font enFont = new Font(fonten, Font.PLAIN, size);
				// graphics.setFont(enFont);
				// graphics.drawString(String.format("%02d",
				// calendar1.get(Calendar.MONTH) + 1), 220 + x, 310 + y);
				// }
				// if (lable.equals("日")) {
				// Font enFont = new Font(fonten, Font.PLAIN, size);
				// graphics.setFont(enFont);
				// graphics.drawString(String.format("%02d",
				// calendar1.get(Calendar.DATE)), 210 + x, 310 + y);
				// }
				// }

				if (lable.equals("原账务日期")) {
					if (hcZykbpzyw.getYzwDate() != null) {
						Font enFont = new Font(fonten, Font.PLAIN, size);
						graphics.setFont(enFont);
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(hcZykbpzyw.getYzwDate());
						graphics.drawString(
								String.format("%04d",
										calendar.get(Calendar.YEAR)), x, y);
						graphics.drawString(
								String.format("%02d",
										calendar.get(Calendar.MONTH) + 1),
								x + 65, y);
						graphics.drawString(
								String.format("%02d",
										calendar.get(Calendar.DATE)), x + 110,
								y);
					}
				}

				if (lable.equals("凭证种类代码")) {
					if (hcZykbpzyw.getPzType() != null) {
						Font zhFont = new Font(fontzh, Font.PLAIN, size);
						graphics.setFont(zhFont);
						if (Integer.valueOf(hcZykbpzyw.getPzType()) == 1) {
							graphics.drawString(click, x - 1, y + 2);
						}
						if (Integer.valueOf(hcZykbpzyw.getPzType()) == 2) {
							graphics.drawString(click, 100 + x, 2 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getPzType()) == 3) {
							graphics.drawString(click, 202 + x, y + 2);
						}
						if (Integer.valueOf(hcZykbpzyw.getPzType()) == 4) {
							graphics.drawString(click, 317 + x, 2 + y);
						}
					}
				}
				if (lable.equals("业务种类代码")) {
					if (hcZykbpzyw.getBusinessType() != null) {
						Font zhFont = new Font(fontzh, Font.PLAIN, size);
						graphics.setFont(zhFont);
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 1) {
							graphics.drawString(click, x - 2, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 2) {
							graphics.drawString(click, 83 + x, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 3) {
							graphics.drawString(click, 160 + x, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 4) {
							graphics.drawString(click, 239 + x, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 5) {
							graphics.drawString(click, 317 + x, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 6) {
							graphics.drawString(click, 395 + x, 1 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 7) {
							graphics.drawString(click, x - 2, 17 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 8) {
							graphics.drawString(click, 82 + x, 17 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 9) {
							graphics.drawString(click, 160 + x, 17 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 10) {
							graphics.drawString(click, 239 + x, 17 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getBusinessType()) == 11) {
							graphics.drawString(click, 317 + x, 17 + y);
						}
					}

				}
				if (lable.equals("冲正/补记明细业务种类代码")) {
					if (hcZykbpzyw.getCzbjmxywType() != null) {
						Font zhFont = new Font(fontzh, Font.PLAIN, size);
						graphics.setFont(zhFont);
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 1) {
							graphics.drawString(click, x - 2, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 2) {
							graphics.drawString(click, 83 + x, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 3) {
							graphics.drawString(click, 160 + x, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 4) {
							graphics.drawString(click, 239 + x, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 5) {
							graphics.drawString(click, 317 + x, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 6) {
							graphics.drawString(click, 395 + x, y - 1);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 7) {
							graphics.drawString(click, x - 2, 16 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 8) {
							graphics.drawString(click, 82 + x, 16 + y);
						}
						if (Integer.valueOf(hcZykbpzyw.getCzbjmxywType()) == 9) {
							graphics.drawString(click, 160 + x, 16 + y);
						}
					}
				}
			}
		}
	}

	@Expose
	public String getPrintImageAll(Map<String, Object> param) {
		try {
			if (param == null)
				return null;
			IUser loginuser = ContextHolder.getLoginUser();
			if (loginuser == null)
				throw new NoneLoginException("您未登录，请刷新页面后重试！！");
			param.put("x", 0);
			param.put("y", 0);
			BufferedImage imagepz = ImageIO.read(new File(DoradoContext
					.getCurrent().getServletContext()
					.getRealPath("pz/zykbpz.jpg")));
			Graphics2D graphicspz = (Graphics2D) imagepz.getGraphics();
			setImageData(graphicspz, param);
			// ByteArrayOutputStream bytestreampz=new ByteArrayOutputStream();
			String filename = "pz/temp/all_" + loginuser.getUsername() + ".jpg";
			FileOutputStream fo = new FileOutputStream(new File(DoradoContext
					.getCurrent().getServletContext().getRealPath(filename)));
			ImageIO.write(imagepz, "jpg", fo);
			fo.flush();
			fo.close();
			// String
			// str="data:image/png;base64,"+Base64.encodeBase64String(bytestreampz.toByteArray());
			// System.out.println(str.length()+","+bytestreampz.toByteArray().length);
			return filename;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Expose
	public String getPrintImageData(Map<String, Object> param) {
		try {
			if (param == null)
				return null;
			IUser loginuser = ContextHolder.getLoginUser();
			if (loginuser == null)
				throw new NoneLoginException("您未登录，请刷新页面后重试！！");
			param.put("x", -25);
			param.put("y", -37);
			BufferedImage imagedata = ImageIO.read(new File(DoradoContext
					.getCurrent().getServletContext()
					.getRealPath("pz/zykbpz.jpg")));
			// System.out.println(imagepz.getWidth()*1.0/96.0*24.5);
			// BufferedImage imagedata= new BufferedImage(imagepz.getWidth(),
			// imagepz.getHeight(), imagepz.getType());
			Graphics2D graphicsdata = (Graphics2D) imagedata.getGraphics();
			graphicsdata.setBackground(Color.WHITE);
			// 打印背景图片
			graphicsdata.clearRect(0, 0, imagedata.getWidth(),
					imagedata.getHeight());
			setImageData(graphicsdata, param);
			// ByteArrayOutputStream bytestreampz=new ByteArrayOutputStream();
			String filename = "pz/temp/data_" + loginuser.getUsername()
					+ ".jpg";
			FileOutputStream fo = new FileOutputStream(new File(DoradoContext
					.getCurrent().getServletContext().getRealPath(filename)));
			ImageIO.write(imagedata, "jpg", fo);
			fo.flush();
			fo.close();
			// String
			// str="data:image/png;base64,"+Base64.encodeBase64String(bytestreampz.toByteArray());
			// System.out.println(str.length()+","+bytestreampz.toByteArray().length);
			return filename;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Expose
	public Map<String, Object> getUserInfo() {
		IUser loginuser = ContextHolder.getLoginUser();
		Map<String, Object> userinfo = new HashMap<String, Object>();
		if (loginuser == null) {
			userinfo.put("cname", "");
			userinfo.put("dept", "");
			return userinfo;
		}
		userinfo.put("cname", loginuser.getCname());
		if (loginuser.getDepts() == null || loginuser.getDepts().size() <= 0) {
			userinfo.put("dept", "");
			return userinfo;
		}
		userinfo.put("dept", loginuser.getDepts().get(0).getName());
		return userinfo;
	}

	@DataProvider
	public Collection<HcZykbpzyw> getTypeByGroup() {
		return zykbpzywDao.getOneGroupByType();
	}

	@DataProvider
	public Collection<HcZykbpzyw> getTepementNameByType(
			Map<String, Object> param) {
		if (param == null) {
			return null;
		}
		String type = (String) param.get("type");
		return zykbpzywDao.getCoByType(type);
	}

	@Expose
	public HcZykbpzyw ajaxselectOneByTypeAndTemplate(Map<String, Object> param) {
		String type = (String) param.get("type");
		String pzTemplate = (String) param.get("pzTemplate");
		if (type == null || pzTemplate == null) {
			return null;
		}
		return zykbpzywDao.selectOneByTypeAndTemplate(type, pzTemplate);
	}

	@DataProvider
	public List<Record> getFonts() {
		return zykbpzywDao.getFonts();
	}

	@DataProvider
	public PzFont getFont() {
		return zykbpzywDao.getFont(PZ_CODE_);
	}

	@Transactional
	@DataResolver
	public void saveXjpz(HcZykbpzyw hcZykbpzyw) {
		zykbpzywDao.saveXjpz(hcZykbpzyw);
	}

	@Transactional
	@DataResolver
	public void saveXjpzXY(Collection<HcpzXY> hcXjpzXY) {
		zykbpzywDao.saveXjpzXY(hcXjpzXY);
	}

	@Transactional
	@DataResolver
	public void saveFont(PzFont pzFont) throws Exception {
		zykbpzywDao.updateFont(pzFont);
	}

	@Expose
	public void deleteTemplateById(String id) {
		if (StringUtils.isNotEmpty(id)) {
			zykbpzywDao.deleteTemplateById(id);
		}
	}

	@Expose
	public Account ajaxActionGetName(Map<String, Object> parameter) {
		String account = (String) parameter.get("account");
		return zykbpzywDao.getAccountName(account);
	}

	@Expose
	public boolean updateTempalteName(Map<String, Object> parameter) {
		String id = (String) parameter.get("id");
		String pzTemplate = (String) parameter.get("pzTemplate");
		return zykbpzywDao.updateTemplateNameDao(pzTemplate, id);
	}

	@Expose
	public boolean updateTempalteType(Map<String, Object> parameter) {
		String id = (String) parameter.get("id");
		String type = (String) parameter.get("type");
		return zykbpzywDao.updateTemplateTypeDao(type, id);
	}

}
